<template>
  <div class="chat-room" @keyup.enter="sendMessage()" v-if="show">
    <div class="chat-header">聊天室</div>
    <div class="chat-messages">
      <div class="message" v-for="(message, index) in messages" :key="index">
        {{ message.userName }}:{{ message.msg }}
      </div>
    </div>
    <div class="chat-input">
      <input type="text" class="chat-input-text" v-model="newMessage" placeholder="输入消息..." />
      <button id="voiceInput" @mousedown="startSpeaking" @mouseup="endSpeaking">语音输入</button>
      <button @click="sendMessage()">发送</button>
    </div>
    <div class="button-container">
      <img class="exit-button" src="../../assets/path.png" alt="退出" @click="handleExit()" />
    </div>
  </div>
  <speaking ref="speakingBox" />
</template>

<script setup>
import speaking from './speaking.vue'
import store from "@/store";
import { onUnmounted, ref } from "vue";
import { useWebsocket } from "../../hooks";

const messages = ref([]);
const newMessage = ref("");
const socket = useWebsocket(handelMessage); //连接socket
const userData = store.state.userData;
const roomData = store.state.roomData;
const show = ref(false);
const speakingBox = ref(null)

/* 
    发出消息格式
    {
        userId,
        userName,
        roomId,
        msg,
    }
*/

onUnmounted(() => {
  socket.close();
});

function handleExit() {
  show.value = !show.value;
}

function sendMessage() {
  if (newMessage.value.trim()) {
    //先把自己的消息发到socket中
    const userMessage = {
      userId: userData.userId,
      userName: userData.username,
      roomId: roomData.roomId,
      msg: newMessage.value,
      type: "message",
    };
    messages.value.push(userMessage);
    socket.send(JSON.stringify(userMessage))
    newMessage.value = "";
  }
}

function handelMessage(event) {
  const data = JSON.parse(event.data);
  //防止用户重复收到消息
  if (userData.userId == data.userId) {
    return;
  }
  console.log("socket发送的" + data.type, data);
  switch (data.type) {
    case "message":
      messages.value.push(data);
      break;
  }
}

//开始语音输入
function startSpeaking() {
  speakingBox.value.showRecordingIndicator();
}

//关闭语音输入
function endSpeaking() {
  speakingBox.value.hideRecordingIndicator();
}

defineExpose({
  handleExit,
});
</script>

<style scoped>
.button-container {
  display: flex;
  position: fixed;
  right: 2rem;
  top: 2rem;
}

.chat-room {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  /* 将头部和底部的内容分别放在顶部和底部 */
  align-items: center;
  height: 40rem;
  width: 30rem;
  margin: 0;
  padding: 20px;
  box-sizing: border-box;
  border-radius: 20px;
  background-color: rgb(255, 255, 255);
  box-shadow: 0px 4px 6px gray;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000000000;
}

.chat-header {
  padding: 10px 0;
  text-align: center;
}

.chat-messages {
  flex: 1;
  /* 占据尽可能多的空间 */
  padding: 10px;
  height: 30rem;
  width: 22.5rem;
  overflow-y: auto;
  /* 滚动显示更多消息 */
  position: fixed;
  top: 3.125rem;
  left: 3rem;
}

.message {
  padding: 5px 0;
}

.chat-input {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #fff;
  border-top: 1px solid #ccc;
  /* 可选：添加顶部边框以分隔消息和输入框 */
}

.chat-input-text {
  flex: 1;
  /* 宽度与容器相同 */
  padding: 5px;
  border: none;
  /* 点击时没有边框 */
  border-radius: 5px 0 0 5px;
  /* 左侧圆角 */
}

.chat-input button {
  padding: 5px 10px;
  border: none;
  background-color: #4caf50;
  color: white;
  cursor: pointer;
  border-radius: 0 5px 5px 0;
  /* 右侧圆角 */
}

#voiceInput {
  border-radius: 0px;
  /* 无侧圆角 */
  margin-right: 2px;
}

/* 可选：添加focus样式，以便在聚焦时显示边框（如果需要） */
.chat-input-text:focus {
  outline: none;
  /* 移除默认的聚焦边框 */
  /* 如果需要边框，可以添加自定义边框样式 */
  /* border: 1px solid #007bff; */
}
</style>